package ar.fiuba.logger;

/**
 * The Class LogEnvironmentInformation.
 * 
 * This class implements functions to load environment information needed for the logger such as 
 * caller file name, caller method name, caller line number and thread name.
 */
public class LogEnvironmentInformation {

  /** The method name. */
  private String methodName;

  /** The file name. */
  private String fileName;

  /** The line number. */
  private int lineNumber;

  /** The thread name. */
  private String threadName;

  /**
   * Instantiates a new log environment information object.
   * 
   * @param stackDepth
   *            the stack depth as seen from the method that builds this object.
   *            This value will be used to load the environment information from the stack.
   */
  public LogEnvironmentInformation(int stackDepth) {
    /* Here we add +2 to the stack depth so 0 means current method, 1 means the caller method, etc... */

    final StackTraceElement[] ste = Thread.currentThread().getStackTrace();

    stackDepth = stackDepth + 2;

    methodName = ste[stackDepth].getMethodName();

    fileName = ste[stackDepth].getFileName();

    lineNumber = ste[stackDepth].getLineNumber();

    threadName = Thread.currentThread().getName();
  }

  /* (non-Javadoc)
   * @see java.lang.Object#equals(java.lang.Object)
   */
  @Override
  public boolean equals(Object obj) {
    if (this == obj)
      return true;
    if (obj == null)
      return false;
    if (getClass() != obj.getClass())
      return false;
    LogEnvironmentInformation other = (LogEnvironmentInformation) obj;
    if (fileName == null) {
      if (other.fileName != null)
        return false;
    } else if (!fileName.equals(other.fileName))
      return false;
    if (lineNumber != other.lineNumber)
      return false;
    if (methodName == null) {
      if (other.methodName != null)
        return false;
    } else if (!methodName.equals(other.methodName))
      return false;
    if (threadName == null) {
      if (other.threadName != null)
        return false;
    } else if (!threadName.equals(other.threadName))
      return false;
    return true;
  }

  /**
   * Gets the file name.
   * 
   * @return the file name
   */
  public String getFileName() {
    return fileName;
  }

  /**
   * Gets the line number.
   * 
   * @return the line number
   */
  public int getLineNumber() {
    return lineNumber;
  }

  /**
   * Gets the method name.
   * 
   * @return the method name
   */
  public String getMethodName() {
    return methodName;
  }

  /**
   * Gets the thread name.
   *
   * @return the thread name
   */
  public String getThreadName() {
    return threadName;
  }

  /* (non-Javadoc)
   * @see java.lang.Object#hashCode()
   */
  @Override
  public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((fileName == null) ? 0 : fileName.hashCode());
    result = prime * result + lineNumber;
    result = prime * result + ((methodName == null) ? 0 : methodName.hashCode());
    result = prime * result + ((threadName == null) ? 0 : threadName.hashCode());
    return result;
  }
}
